package com.jn.retrofit.adapter;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.jn.retrofit.R;
import com.jn.retrofit.entity.HotSearch;

import java.util.ArrayList;
import java.util.List;

public class HotSearchAdapter extends RecyclerView.Adapter<HotSearchAdapter.ViewHolder> {
    private List<HotSearch.HotSearchItem> list;

    public HotSearchAdapter() {
        list = new ArrayList<>();
    }



    public void setList(List<HotSearch.HotSearchItem> list) {
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hot_search, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        HotSearch.HotSearchItem item = list.get(position);
        holder.title.setText(item.title);
        holder.hotValue.setText(item.hotValue);

        if (!TextUtils.isEmpty(item.link)) {
            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(item.link));
                holder.itemView.getContext().startActivity(intent);
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView hotValue;

        ViewHolder(View view) {
            super(view);
            title = view.findViewById(R.id.title);
            hotValue = view.findViewById(R.id.hotValue);
        }
    }
}